#include "GY_LineWidthComboBox.h"

GY_LineWidthComboBox::GY_LineWidthComboBox(QWidget *parent)
    :QComboBox(parent), m_drawType(1)
{
    m_pPenEdit = new GY_PenWidget();
    m_pListWidget = new QListWidget();
    setContextMenuPolicy(Qt::NoContextMenu); //禁用右键菜单
//    m_pListWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //禁用垂直滚动条
    setLineEdit(m_pPenEdit);
    setModel(m_pListWidget->model());
    setView(m_pListWidget);


}

GY_LineWidthComboBox::~GY_LineWidthComboBox()
{

}

void GY_LineWidthComboBox::setType(int drawType)
{
    m_drawType = drawType;
}

int GY_LineWidthComboBox::getCurrentIndex()
{
    return m_drawType;
}

void GY_LineWidthComboBox::setCurrentIndex(int index)
{
    m_index = index;
    m_pPenEdit->updataPen(index, m_drawType);
}

void GY_LineWidthComboBox::setList(QList<int> &list)
{
    m_list = list;
    m_pListWidget->clear();
    for (int i=0;i<m_list.size();i++) {
        appendItem(m_list[i]);
    }
}

QList<int> GY_LineWidthComboBox::getList()
{
    return m_list;
}

/**
 * @brief appendItem 添加线条样式
 * @param index 索引
 */
void GY_LineWidthComboBox::appendItem(const int &index)
{
    GY_PenWidget* pWidget = new GY_PenWidget(this);
    pWidget->updataPen(index,m_drawType);
//    pWidget->setFixedSize(20,20);
    connect(pWidget,SIGNAL(sendClicked(const int&)),this,SLOT(onClickPenWidget(const int &)));

    QListWidgetItem* pItem = new QListWidgetItem(m_pListWidget);
    m_pListWidget->addItem(pItem);
    m_pListWidget->setItemWidget(pItem,pWidget);
}

/**
 * @brief onClickPenWidget 点击响应
 * @param index
 */
void GY_LineWidthComboBox::onClickPenWidget(const int &index)
{
    m_index = index;
    m_pPenEdit->updataPen(index,m_drawType);
    hidePopup();
    emit selectedItemChanged(m_index);
}
